package com.java.controller;


import com.java.model.IndexSlide;
import com.java.model.MediaAccount;
import com.java.model.Result;
import com.java.service.IndexSlideService;
import com.java.service.MediaAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

@Controller
@RequestMapping("/operating")
public class OperatingController {


    String urlpat;

    @Autowired
    private IndexSlideService indexSlideService;

    @Autowired
    private MediaAccountService mediaAccountService;

    @GetMapping("/slide")
    public String slide(){
        return "/operating/slide";
    }

    @PostMapping("/list")
    @ResponseBody
    public Result list(IndexSlide indexSlide){
        return indexSlideService.list(indexSlide);
    }



    /**
     * 上传图片
     * @param request
     * @param file
     * @return
     */
    @RequestMapping("/files")
    @ResponseBody
    // 文件上传
    public Map<String,Object> felts(HttpServletRequest request, @RequestParam MultipartFile file) {

        Map<String,Object> mv=new HashMap<String, Object>();
        Map<String,String> mvv=new HashMap<String, String>();
        try {
            String rootPath = request.getSession().getServletContext().getRealPath("/image/");
            Calendar date = Calendar.getInstance(); //Calendar.getInstance()是获取一个Calendar对象并可以进行时间的计算，时区的指定
            String originalFile = file.getOriginalFilename(); //获得文件最初的路径
            String uuid = UUID.randomUUID().toString();    //UUID转化为String对象
            String newfilename=date.get(Calendar.YEAR)+""+(date.get(Calendar.MONTH)+1)+""+date.get(Calendar.DATE)+uuid.replace("-", "")+originalFile;
            //得到完整路径名
            File newFile = new File(rootPath+newfilename);
            /*文件不存在就创建*/
            if(!newFile.getParentFile().exists()){
                newFile.getParentFile().mkdirs();
            }
            String filename=originalFile.substring(0, originalFile.indexOf("."));
            System.out.println(originalFile);
            System.out.println(filename);
            file.transferTo(newFile);
            System.out.println("newFile : "+newFile);
            urlpat= "/image/"+ newfilename;
            mvv.put("src", urlpat);
            mvv.put("title",newfilename);
            mv.put("code", 0);
            mv.put("msg", "上传成功");
            mv.put("data", mvv);
            return mv;
        } catch (Exception e) {
            e.printStackTrace();
            mv.put("success", 1);
            return mv;
        }
    }

    /**
     * 跳转轮播图新增页面
     * @return
     */
    @GetMapping("/slideAdd")
    public String add() {
        return "operating/slideAdd";
    }

    /**
     * 转轮播图新增
     * @param indexSlide
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody IndexSlide indexSlide) {
        indexSlide.setImagePath(urlpat);
        indexSlide.setCreateTime(new Date());
        indexSlideService.save(indexSlide);
        return Result.success();
    }
    /**
     * 跳转到轮播图编辑访问顾问
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/slideEdit")
    public String edit(int id, Model model) {
        Result result = indexSlideService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("IndexSlide", result.getData());
        }

        return "/operating/slideEdit";
    }

    /**
     * 轮播图编辑访问顾问
     * @param indexSlide
     * @return
     */
    @PostMapping("/modify")
    @ResponseBody
    public Result remove(@RequestBody IndexSlide indexSlide) {
        indexSlide.setImagePath(urlpat);
        indexSlide.setCreateTime(new Date());
        indexSlideService.modify(indexSlide);
        return Result.success();
    }


    /**
     * 轮播图删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(int[] ids) {
        indexSlideService.remove(ids);
        return Result.success();
    }


    /**
     * 轮播图的启用禁用
     * @return
     */
    @PostMapping("/indexSlideStatus")
    @ResponseBody
    public Result indexSlideStatus(int[] ids, String status) {
        return indexSlideService.indexSlideStatus(ids, status);
    }

    /**
     * 跳转到首页资源页面
     * @return
     */
    @GetMapping("/resource")
    public String resource(){
        return "operating/resource";
    }


    /**
     * 首页资源查询
     * @param mediaAccount
     * @return
     */
    @PostMapping("/toResource")
    @ResponseBody
    public Result toResource(MediaAccount mediaAccount){
        return mediaAccountService.resource(mediaAccount);
    }

    /**
     * 跳转到首页资源管理页面
     * @return
     */
    @GetMapping("/resourceManagement")
    public String ResourceManagement(Model model, String name){
        model.addAttribute("name", name);
        return "operating/resourceManagement";
    }

    /**
     * 首页资源管理页面查询
     * @param mediaAccount
     * @return
     */
    @PostMapping("/toResourceManagement")
    @ResponseBody
    public Result toResourceManagement(MediaAccount mediaAccount){
        return mediaAccountService.resourceList(mediaAccount);
    }

    @PostMapping("/resourceRemove")
    @ResponseBody
    public Result resourceRemove(int[] ids) {
        mediaAccountService.resourceRemove(ids);
        return Result.success();
    }

}
